package com.lu.system.user.service.impl;

import com.lu.common.utils.BuildTree;
import com.lu.system.user.domain.Tree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.lu.system.user.dao.MenuDao;
import com.lu.system.user.domain.MenuDO;
import com.lu.system.user.service.MenuService;

@Service
public class MenuServiceImpl implements MenuService {
	@Autowired
	private MenuDao menuDao;

	@Override
	public MenuDO get(Long menuId){
		return menuDao.get(menuId);
	}
	
	@Override
	public List<MenuDO> list(Map<String, Object> map){
		return menuDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return menuDao.count(map);
	}
	
	@Override
	public int save(MenuDO menu){
		return menuDao.save(menu);
	}

    @Override
    public void saveBatch(List<MenuDO> menu) throws Exception {
		menuDao.saveBatch(menu);
    }
	
	@Override
	public int update(MenuDO menu){
		return menuDao.update(menu);
	}

    @Override
    public void updateBatch(List<MenuDO> menu) throws Exception {
		menuDao.updateBatch(menu);
    }
	
	@Override
	public int remove(Long menuId){
		return menuDao.remove(menuId);
	}
	
	@Override
	public int batchRemove(Long[] menuIds){
		return menuDao.batchRemove(menuIds);
	}

	@Override
	public List<Tree<MenuDO>> listMenuTree(Long id) {
		List<Tree<MenuDO>> trees = new ArrayList<Tree<MenuDO>>();
		List<MenuDO> menuDOs = menuDao.listMenuByUserId(id);

		for (MenuDO sysMenuDO : menuDOs) {
			Tree<MenuDO> tree = new Tree<MenuDO>();
			tree.setId(sysMenuDO.getMenuId().toString());
			tree.setParentId(sysMenuDO.getParentId().toString());
			tree.setTitle(sysMenuDO.getName());
			tree.setIcon(sysMenuDO.getIcon());
			tree.setQuanx(sysMenuDO.getPerms());
			tree.setJump(sysMenuDO.getUrl());
			trees.add(tree);
			System.out.println(trees);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		List<Tree<MenuDO>> lists = BuildTree.build(trees, "0");
		System.out.println(lists.toString());
		return lists;
	}
	
}
